package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.farmtrim;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.module.zl;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.service.ZlService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("zl")
public class ZlControl {

	@Autowired
	ZlService zlService;

	@Autowired
	CodeService codeService;

	@Autowired
	TeaxService teaxService;

	@Autowired
	TeaService teaService;
	
	@Autowired
	UserService userService;

	List<code> codelist;
	List<code> teatypelist;

	@RequestMapping("info")
	public String info(Model m) {
		codelist = codeService.selectbyType("08");

		teatypelist = codeService.selectbyType("200");
		m.addAttribute("codelist", codelist);
		m.addAttribute("type", teatypelist);
		return "zl/zl";
	}

	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, zl zl) {
		List<zl> list = zlService.selectpage(start, limit, zl);
		List<zl> total = zlService.selectotal(zl);

		for (zl zl2 : list) {
			for (code c : codelist) {
				if (zl2.getZlResult() == c.getCodeValue())
					zl2.setResult(c);

			}
			for (code c : teatypelist) {
				if (zl2.getZlType() == c.getCodeValue())
					zl2.setType(c);
			}
			if (zl2.getZlType() == 0) {
				zl2.setWarehouseteax(teaxService.selectbyid(zl2.getZlTeaid()));
			} else {
				zl2.setWarehousetea(teaService.selectbyid(zl2.getZlTeaid()));
			}
			zl2.setUser(userService.selectuserbyid(zl2.getZlUserid()));

		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size());
		return page;

	}
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
		zl zl=new zl();
		zl.setZlType(-1);
		zl.setZlResult(-1);
 		List<cn.com.hfuu.module.zl> list = zlService.selectotal(zl);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (zl z : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			for (code c : codelist) {
				if (z.getZlResult() == c.getCodeValue())
					z.setResult(c);

			}
			for (code c : teatypelist) {
				if (z.getZlType() == c.getCodeValue())
					z.setType(c);
			}
			if (z.getZlType() == 0) {
				z.setWarehouseteax(teaxService.selectbyid(z.getZlTeaid()));
			} else {
				z.setWarehousetea(teaService.selectbyid(z.getZlTeaid()));
			}
			z.setUser(userService.selectuserbyid(z.getZlUserid()));
			
			map1.put("编号", z.getZlId());
			if (z.getZlType() == 0) {
				map1.put("茶名",z.getWarehouseteax().getWarehouseTeaname());
				map1.put("茶叶编号",z.getWarehouseteax().getWarehouseTeaxid());
			} else {
				map1.put("茶名",z.getWarehousetea().getTeaName());
				map1.put("茶叶编号",z.getWarehousetea().getWarehouseTeaid());

			}
			map1.put("类型",z.getType().getCodeName());
			
			map1.put("结果",z.getResult().getCodeName());

			map1.put("操作人", z.getUser().getUserName());
			map1.put("操作时间", z.getZlTime());
			mapList.add(map1);
		}
		String[] title = { "编号", "茶名", "茶叶编号","类型","结果","操作人","操作时间" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "质量信息");
		return new ModelAndView(ve, map);
	}
	

	@RequestMapping("addteax")
	public String add(Model m) {
		List<warehouseteax> teaxlist = teaxService.selectallzl();
		m.addAttribute("list", teaxlist);
		m.addAttribute("codelist", codelist);
		return "zl/addteax";

	}

	@RequestMapping("addtea")
	public String addtea(Model m) {
		m.addAttribute("codelist", codelist);
		List<warehousetea> tealist = teaService.selectallzl();
		m.addAttribute("list", tealist);
		return "zl/addtea";
	}

	@RequestMapping("addteaproc")
	@ResponseBody
	public String addteaproc(zl zl, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		zl.setZlId(idu.getUuid());
		zl.setZlUserid(u.getUserId());
		zl.setZlTime(sys.getDatetime());
		zl.setZlType(1);
		if (zlService.insert(zl) > 0) {
			warehousetea tea=teaService.selectbyid(zl.getZlTeaid());
			tea.setTeaZlid(zl.getZlId());
			teaService.update(tea);
			return "success";
		}
		return null;

	}

	@RequestMapping("addteaxproc")
	@ResponseBody
	public String addteaxproc(zl zl, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		zl.setZlId(idu.getUuid());
		zl.setZlUserid(u.getUserId());
		zl.setZlTime(sys.getDatetime());
		zl.setZlType(0);
		if (zlService.insert(zl) > 0) {
			warehouseteax teax=teaxService.selectbyid(zl.getZlTeaid());
			teax.setWarehouseZlid(zl.getZlId());
			teaxService.update(teax);
			return "success";

		}
		return null;

	}
	
	@RequestMapping("edit")
	public ModelAndView edit(String id){
		ModelAndView mv=new ModelAndView();
		zl zl=zlService.selectbyid(id);
		
		if(zl.getZlType()==0){
			List<warehouseteax> list=teaxService.selectall();
			mv.addObject("list", list);
			mv.setViewName("zl/editteax");
		}else{
			List<warehousetea> list=teaService.selectall();
			mv.addObject("list", list);
			mv.setViewName("zl/edittea");

		}
		mv.addObject("zl", zl);
		mv.addObject("codelist", codelist);
		return mv;
		
	}
	
	@RequestMapping("editteaproc")
	@ResponseBody
	public String editteaproc(zl zl){
		zl old=zlService.selectbyid(zl.getZlId());
		if(zl.getZlTeaid().equals(old.getZlTeaid())){//检测材料未更换
			if(zlService.update(zl)>0){
				return "success";
			}
			
		}else{//检测材料更换
			//warehousetea teaold=teaService.selectbyid(old.getZlTeaid());
			warehousetea teanew=teaService.selectbyid(zl.getZlTeaid());
			teaService.setidzlnull(old.getZlTeaid());
			teanew.setTeaZlid(zl.getZlId());
			teaService.update(teanew);
			if(zlService.update(zl)>0){
				return "success";
			}
		}
		
		
		
		return null;
		
	}
	
	
	@RequestMapping("editteaxproc")
	@ResponseBody
	public String editteaxproc(zl zl){
		zl old=zlService.selectbyid(zl.getZlId());
		if(zl.getZlTeaid().equals(old.getZlTeaid())){//检测材料未更换
			if(zlService.update(zl)>0){
				return "success";
			}
			
		}else{//检测材料更换
			//warehousetea teaold=teaService.selectbyid(old.getZlTeaid());
			warehouseteax teaxnew=teaxService.selectbyid(zl.getZlTeaid());
			teaxService.setidzlnull(old.getZlTeaid());
			teaxnew.setWarehouseZlid(zl.getZlId());
			teaxService.update(teaxnew);
			if(zlService.update(zl)>0){
				return "success";
			}
		}
		return null;
		
	}

	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String[] ids){
		for (int i = 0; i < ids.length; i++) {
			zl zl=zlService.selectbyid(ids[i]);
			if(zl.getZlType()==0){
				teaxService.updatezlnull(ids[i]);
				zlService.delete(ids[i]);
			}else{
				teaService.updatezlnull(ids[i]);
				zlService.delete(ids[i]);
			}
		}
		return "success";
		
	}
}
